-- 主库 名字 z9 , 下面包含子库
-- socket
-- buffer
-- log
-- event_loop
-- packet
-- redis

package.path = package.path .. ";;;./?.lua;./framework/?.lua;./framework/lib/?.lua;./framework/lib/sproto/?.lua;"
package.cpath = package.cpath .. ";;;./framework/lib/?.so;"

z9 = require("z9")

require("functions")

z9.print_r = require("print_r")
z9.sproto = require("sproto")
z9.sprotoparser = require("sprotoparser")
z9.const = require("const")
z9.md5 = require("md5.md5")
z9.ini = require("ini")
z9.json = require("json")
z9.csv = require("csv")

z9.HTTP = require("http")  --区别于 http.h 导入到lua的z9.http
z9.redisdb = require("redisdb")
z9.lumen = require("minilumen.init")

z9.rpc = require("rpc")
z9.sessionBase = require("sessionBase")
z9.sessionTcp = require("sessionTcp")
z9.sessionHttp = require("sessionHttp")
z9.actionBase = require("actionBase")
z9.httpclient = require("httpclient")
z9.app = require("application")


z9.log.Info = function(...)
    z9.log.syslog(z9.log.info, ...)
end 
z9.log.Debug = function(...)
    z9.log.syslog(z9.log.debug, ...)
end 
z9.log.Warn = function(...)
    z9.log.syslog(z9.log.warning, ...)
end
z9.log.Error = function(...)
    z9.log.syslog(z9.log.error, ...)
end
z9.log.Criti = function(...)
    z9.log.syslog(z9.log.critical, ...)
end

z9.HTTP.say = function(funcResp, str)
    funcResp:setHeader("Content-Type","text/plain")
    funcResp:appendBody(str)
    funcResp:finish("200", "OK")
end

local function loadConf()
    config = {}
    config.protocol = "sproto"
    config.server_ip = "106.14.38.203"
    config.server_port = 9999

    config.redis_ip = "106.14.38.203"
    config.redis_port = 6666

    config.area = 1 --1服 
end 

local function loadCsv()

end 

local function loadSproto()
    local types = io.readfile("./configs/rpc/types.sproto")
    local c2s = io.readfile("./configs/rpc/c2s.sproto")
    local s2c = io.readfile("./configs/rpc/s2c.sproto")

    z9.proto = {}
    z9.proto.types = z9.sprotoparser.parse(types)
    z9.proto.c2s = z9.sprotoparser.parse(types .. c2s)
    z9.proto.s2c = z9.sprotoparser.parse(types .. s2c)

    if CLIENT then
        z9.log.Info("This is Client")
        --for client test
        z9.proto.server = z9.sproto.new(z9.proto.s2c):host("package")
        z9.proto.encode = z9.proto.server:attach(z9.sproto.new(z9.proto.c2s))
        z9.proto.decode = function(data)
            return z9.proto.server:dispatch(data)
        end 
    else
        --for server
        z9.proto.server = z9.sproto.new(z9.proto.c2s):host("package")
        z9.proto.encode = z9.proto.server:attach(z9.sproto.new(z9.proto.s2c))  --encode(strCmd, tabMessage) --encode("chat_update",{index=1})
        z9.proto.decode = function(data)
            return z9.proto.server:dispatch(data)
        end
    end 
    
end 

local function loadDb()
    --db = redisdb:create(loop, config.redis_ip, config.redis_port)
end  

loadConf()
loadCsv()
loadSproto()
